using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class CirclePagePicsCtrl : UserControl
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "CirclePagePicsCtrl";
		private const double cDSigPicH = 300.0;
		private const double cDSigPicW = 300.0;
		private const double cDMulPicH = 110.0;
		private const double cDMulPicW = 116.0;
		private const double cDMulPicHToW = 1.1666666666666667;
		private const double cDMulPicMaxH = 280.0;
		private const double cDMulPicMaxW = 240.0;
		private const int cNMaxColCnt4 = 2;
		private const int cNMaxCacheCnt = 5;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(List<TloMedia>), typeof(CirclePagePicsCtrl), new PropertyMetadata(null, new PropertyChangedCallback(CirclePagePicsCtrl.OnItemObjectPropertyChanged)));
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchxxx;
		private object mObjectWatcher;
		public List<CirclePagePicItem> mListItems = new List<CirclePagePicItem>();
		private int mNImgCnt;
		private int cNImgRowMaxCnt = 3;
		private int cNImgColMaxCnt = 3;
		private static List<CirclePagePicsCtrl> sTimelineItem = new List<CirclePagePicsCtrl>();
		internal StackPanel panelRoot;
		private bool _contentLoaded;
		public event CirclePagePicsCtrl.ListItemClick ItemClick;
		public List<TloMedia> ItemObject
		{
			get
			{
				return (List<TloMedia>)base.GetValue(CirclePagePicsCtrl.ItemObjectProperty);
			}
			set
			{
				base.SetValue(CirclePagePicsCtrl.ItemObjectProperty, value);
			}
		}
		public CirclePagePicsCtrl()
		{
			ShowObjectInMemory.AddOneObjectRecord(this);
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		~CirclePagePicsCtrl()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == CirclePagePicsCtrl.ItemObjectProperty)
			{
				(dependencyObject_0 as CirclePagePicsCtrl).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.RefreshItem();
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void registerEvent()
		{
			if (this.mWatchxxx == null)
			{
				this.mWatchxxx = new EventWatcher(this, null, new EventHandlerDelegate(this.Onxxx));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchxxx != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
				this.mWatchxxx = null;
			}
		}
		private void Onxxx(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
		}
		public void OnVisible()
		{
			using (List<CirclePagePicItem>.Enumerator enumerator = this.mListItems.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					CirclePagePicItem current = enumerator.get_Current();
					if (current != null)
					{
						current.setImgPlayedOn();
						current.ShowImg();
					}
				}
			}
		}
		private void btnLayoutRoot_Click(object sender, RoutedEventArgs e)
		{
		}
		private void picItemClick(object sender, object object_0)
		{
		}
		private void RefreshItem()
		{
			if (this.ItemObject == null)
			{
				return;
			}
			int count = this.ItemObject.get_Count();
			if (count == 4)
			{
				if (this.mNImgCnt != 4)
				{
					if (this.mNImgCnt > 2)
					{
						this.setImgCtrlCnt(0);
					}
					this.cNImgRowMaxCnt = 2;
					this.cNImgColMaxCnt = 2;
					this.setImgCtrlCnt(4);
				}
			}
			else
			{
				if (this.mNImgCnt == 4)
				{
					this.setImgCtrlCnt(2);
					this.cNImgRowMaxCnt = 3;
					this.cNImgColMaxCnt = 3;
					this.setImgCtrlCnt(count);
				}
				else
				{
					this.cNImgRowMaxCnt = 3;
					this.cNImgColMaxCnt = 3;
					this.setImgCtrlCnt(count);
				}
			}
			this.setImgToItem(this.ItemObject);
		}
		private int getImgCtrlCnt()
		{
			int num = 0;
			using (IEnumerator<UIElement> enumerator = this.panelRoot.get_Children().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					UIElement current = enumerator.get_Current();
					if (current is StackPanel)
					{
						StackPanel stackPanel = current as StackPanel;
						for (int i = 0; i < stackPanel.get_Children().get_Count(); i++)
						{
							num++;
						}
					}
				}
			}
			return num;
		}
		public void RunTest()
		{
			this.setImgCtrlCnt(1);
			this.getImgCtrlCnt();
			this.setImgCtrlCnt(6);
			this.getImgCtrlCnt();
			this.setImgCtrlCnt(3);
			this.getImgCtrlCnt();
			this.setImgCtrlCnt(1);
			this.getImgCtrlCnt();
			this.setImgCtrlCnt(9);
			this.getImgCtrlCnt();
			this.setImgCtrlCnt(5);
			this.getImgCtrlCnt();
			this.setImgCtrlCnt(1);
			this.getImgCtrlCnt();
		}
		private void addPicItem(CirclePagePicItem item)
		{
			if (!this.mListItems.Contains(item))
			{
				this.mListItems.Add(item);
				item.ItemClick += new CirclePagePicItem.ListItemClick(this.item_ItemClick);
			}
		}
		private void remPicItem(CirclePagePicItem item)
		{
			this.mListItems.Remove(item);
			item.ItemClick -= new CirclePagePicItem.ListItemClick(this.item_ItemClick);
		}
		private void item_ItemClick(object sender, object object_0)
		{
			BasePage topPage = PageManager.getInstance().GetTopPage();
			if (topPage is CircleMainPage)
			{
				(topPage as CircleMainPage).ShowBigPic(this, (int)object_0);
				return;
			}
			if (topPage is TimelineDetailPage)
			{
				(topPage as TimelineDetailPage).ShowBigPic(this, (int)object_0);
			}
		}
		private void setImgCtrlCnt(int nCnt)
		{
			if (this.mNImgCnt == nCnt)
			{
				return;
			}
			int num = 0;
			using (IEnumerator<UIElement> enumerator = this.panelRoot.get_Children().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					UIElement current = enumerator.get_Current();
					if (current is StackPanel)
					{
						StackPanel stackPanel = current as StackPanel;
						for (int i = 0; i < stackPanel.get_Children().get_Count(); i++)
						{
							UIElement uIElement = stackPanel.get_Children().get_Item(i);
							num++;
							if (num > nCnt)
							{
								this.remPicItem(uIElement as CirclePagePicItem);
								stackPanel.get_Children().Remove(uIElement);
								CirclePagePicItem.PushOneItem(uIElement as CirclePagePicItem);
								i--;
							}
						}
					}
				}
			}
			this.mNImgCnt = nCnt;
			if (nCnt > num)
			{
				this.addImg(nCnt - num);
			}
		}
		private void addImg(int nCntToAdd)
		{
			using (IEnumerator<UIElement> enumerator = this.panelRoot.get_Children().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					UIElement current = enumerator.get_Current();
					if (nCntToAdd <= 0)
					{
						break;
					}
					if (current is StackPanel)
					{
						StackPanel panel = current as StackPanel;
						int num = this.addImgCtrlToPanel(panel, nCntToAdd);
						nCntToAdd = num;
						if (nCntToAdd < 0)
						{
							break;
						}
						if (nCntToAdd == 0)
						{
							break;
						}
					}
				}
			}
		}
		private int addImgCtrlToPanel(StackPanel panel, int nTargetAddCnt)
		{
			if (panel == null)
			{
				return nTargetAddCnt;
			}
			int num = this.cNImgColMaxCnt - panel.get_Children().get_Count();
			int num2 = (num >= nTargetAddCnt) ? nTargetAddCnt : num;
			for (int i = 0; i < num2; i++)
			{
				try
				{
					CirclePagePicItem oneItem = CirclePagePicItem.GetOneItem();
					oneItem.mBIsDelayLoadImage = true;
					this.addPicItem(oneItem);
					panel.get_Children().Add(oneItem);
				}
				catch (Exception)
				{
					if (Debugger.get_IsAttached())
					{
						Debugger.Break();
					}
				}
			}
			return nTargetAddCnt - num2;
		}
		private void setImgToItem(List<TloMedia> listImgs)
		{
			int num = 0;
			StackPanel stackPanel = this.panelRoot.get_Children().get_Item(0) as StackPanel;
			if (stackPanel == null)
			{
				return;
			}
			bool flag = false;
			if (stackPanel.get_Children().get_Count() > 1)
			{
				flag = true;
			}
			double height;
			double width;
			if (flag)
			{
				height = 110.0;
				width = 116.0;
			}
			else
			{
				height = 300.0;
				width = 300.0;
			}
			using (IEnumerator<UIElement> enumerator = this.panelRoot.get_Children().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					UIElement current = enumerator.get_Current();
					if (current is StackPanel)
					{
						StackPanel stackPanel2 = current as StackPanel;
						for (int i = 0; i < stackPanel2.get_Children().get_Count(); i++)
						{
							UIElement uIElement = stackPanel2.get_Children().get_Item(i);
							if (uIElement is CirclePagePicItem)
							{
								CirclePagePicItem circlePagePicItem = uIElement as CirclePagePicItem;
								circlePagePicItem.ObjMediaInfo = this.ItemObject.get_Item(num);
								circlePagePicItem.mNPicIndex = num;
								if (!flag)
								{
									circlePagePicItem.set_Margin(new Thickness(0.0));
									circlePagePicItem.imgContent.set_Stretch(2);
									double num2 = (double)this.ItemObject.get_Item(num).nWidth;
									double num3 = (double)this.ItemObject.get_Item(num).nHeight;
									if (num2 == 0.0)
									{
										goto IL_1AD;
									}
									if (num3 == 0.0)
									{
										goto IL_1AD;
									}
									double num4 = num3 / num2;
									IL_1B8:
									if (num4 > 1.1666666666666667)
									{
										circlePagePicItem.set_Height(280.0);
										circlePagePicItem.set_Width(280.0 / num4);
										goto IL_20D;
									}
									circlePagePicItem.set_Height(240.0 * num4);
									circlePagePicItem.set_Width(240.0);
									goto IL_20D;
									IL_1AD:
									num4 = 1.0;
									goto IL_1B8;
								}
								circlePagePicItem.imgContent.set_Stretch(3);
								circlePagePicItem.set_Margin(new Thickness(1.5, 1.5, 1.5, 1.5));
								circlePagePicItem.set_Height(height);
								circlePagePicItem.set_Width(width);
								IL_20D:
								num++;
							}
						}
					}
				}
			}
		}
		public static CirclePagePicsCtrl GetOneItem()
		{
			if (CirclePagePicsCtrl.sTimelineItem != null && CirclePagePicsCtrl.sTimelineItem.get_Count() != 0)
			{
				CirclePagePicsCtrl result = Enumerable.First<CirclePagePicsCtrl>(CirclePagePicsCtrl.sTimelineItem);
				CirclePagePicsCtrl.sTimelineItem.RemoveAt(0);
				return result;
			}
			return new CirclePagePicsCtrl();
		}
		public static int GetCacheCnt()
		{
			if (CirclePagePicsCtrl.sTimelineItem == null)
			{
				return 0;
			}
			return CirclePagePicsCtrl.sTimelineItem.get_Count();
		}
		public static void PushOneItem(CirclePagePicsCtrl item)
		{
			if (CirclePagePicsCtrl.sTimelineItem == null)
			{
				CirclePagePicsCtrl.sTimelineItem = new List<CirclePagePicsCtrl>();
			}
			item.ClearMemory();
			if (CirclePagePicsCtrl.sTimelineItem.get_Count() > 5)
			{
				return;
			}
			CirclePagePicsCtrl.sTimelineItem.Add(item);
		}
		public void ClearMemory()
		{
			this.setImgCtrlCnt(0);
			this.cNImgRowMaxCnt = 3;
			this.cNImgColMaxCnt = 3;
			this.ItemObject = null;
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Circle/UI/CirclePagePicsCtrl.xaml", 2));
			this.panelRoot = (StackPanel)base.FindName("panelRoot");
		}
	}
}
